/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2025-2025. All rights reserved.
 * Description: utrace infos
 * Author: wuhaoyu
 * Create: 2025-02-10
 */

#include "adapter_trace.h"
#include "dltrace_function.h"
#include "log.h"
#ifndef CCL_KERNEL_AICPU
#include "atrace_api.h"
#else
#include "utrace_api.h"
#endif
using namespace hccl;

HcclResult hrtOpenTrace()
{
    HcclResult ret = DlTraceFunction::GetInstance().DlTraceFunctionInit();
    HCCL_INFO("Call TraceCreate, return value[%d]", ret);
    CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("Call TraceCreate, return value[%d]", ret), HCCL_E_INTERNAL);
    return HCCL_SUCCESS;
}

void hrtTraceDestroy(TraHandle handle)
{
    #ifndef CCL_KERNEL_AICPU
    DlTraceFunction::GetInstance().dlAtraceDestroy(handle);
    #else
    DlTraceFunction::GetInstance().dlUtraceDestroy(handle);
    #endif
    HCCL_INFO("Call TraceDestroy, Params:handle[%p]", handle);
    return;
}

HcclResult hrtTraceSubmit(TraHandle handle, const void *buffer, u32 bufSize)
{
    HcclResult ret = HCCL_E_RESERVED;
    #ifndef CCL_KERNEL_AICPU
    ret = DlTraceFunction::GetInstance().dlAtraceSubmit(handle, buffer, bufSize);
    #else
    ret = DlTraceFunction::GetInstance().dlUtraceSubmit(handle, buffer, bufSize);
    #endif
    CHK_PRT_RET(ret != 0,
        HCCL_ERROR("Call TraceSubmit, return value[%d], Params:handle[%p]", ret, handle),
        HCCL_E_INTERNAL);
    return HCCL_SUCCESS;
}

HcclResult hrtTraceCreateWithAttr(const char *objName, TraHandle &handle)
{
    CHK_PTR_NULL(objName);
    TraHandle ret = TRACE_INVALID_HANDLE;
    #ifndef CCL_KERNEL_AICPU
    TraceAttr hcclAtraceAttr = {0};
    hcclAtraceAttr.exitSave = true;
    hcclAtraceAttr.msgNum = DEFAULT_ATRACE_MSG_NUM;
    hcclAtraceAttr.msgSize = DEFAULT_ATRACE_MSG_SIZE;
    ret = DlTraceFunction::GetInstance().dlAtraceCreateWithAttr(TRACER_TYPE_SCHEDULE,
        objName, &hcclAtraceAttr);
    #else
    TraceAttr hcclUtraceAttr = {0};
    hcclUtraceAttr.exitSave = false;
    hcclUtraceAttr.msgNum = DEFAULT_ATRACE_MSG_NUM;
    hcclUtraceAttr.msgSize = DEFAULT_ATRACE_MSG_SIZE;
    ret = DlTraceFunction::GetInstance().dlUtraceCreateWithAttr(TRACER_TYPE_SCHEDULE,
        objName, &hcclUtraceAttr);
    #endif
    HCCL_INFO("Call TraceCreateWithAttr, return value[%p], Params: objName[%s]", ret, objName);
    CHK_PRT_RET(ret == TRACE_INVALID_HANDLE, HCCL_WARNING("Call TraceCreateWithAttr, return value[%lld]", ret),
        HCCL_E_INTERNAL);
    handle = ret;
    return HCCL_SUCCESS;
}

HcclResult hrtTraceSetGlobalAttr(const TraceGlobalAttr *attr)
{
    #ifdef CCL_KERNEL_AICPU
    TraStatus ret = DlTraceFunction::GetInstance().dlUtraceSetGlobalAttr(attr);
    CHK_PRT_RET(ret != 0,
        HCCL_ERROR("Call TraceSetGlobalAttr, return value[%d]", ret), HCCL_E_INTERNAL);
    #endif
    return HCCL_SUCCESS;
}

HcclResult hrtTraceSave(TracerType tracerType, bool syncFlag)
{
    #ifdef CCL_KERNEL_AICPU
    TraStatus ret = DlTraceFunction::GetInstance().dlUtraceSave(tracerType, syncFlag);
    CHK_PRT_RET(ret != 0, HCCL_ERROR("Call traceSave, return value[%d]", ret), HCCL_E_INTERNAL);
    #endif
    return HCCL_SUCCESS;
}